package tantan.mokuai3.homework;

import tantan.mokuai4.homework.AgeExceotion;
import tantan.mokuai4.homework.IdException;

import java.io.Serializable;
import java.util.Objects;

//编程实现学生类的封装
public class Student implements Serializable {

//    学生的信息有：学号、姓名、年龄
    private String name;
    private String studentId;
    private int age;

//    构造方法
    public Student() {
    }

    public Student(String name, String studentId, int age) throws IdException, AgeExceotion {
        this.name = name;
        setStudentId(studentId);
        setAge(age);
    }

    //    共有的get、set方法
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getStudentId() throws IdException {
            return studentId;
    }

    public void setStudentId(String studentId) throws IdException {
//        合理值判断，如果合理，就对年龄进行修改，不合理则抛出自己写的异常
        if(studentId.matches("\\d{4}")) {
            this.studentId = studentId;
        }else{
            throw new IdException("学号不合理哦！！！");
        }
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) throws AgeExceotion {
//        合理值判断，如果合理，就对年龄进行修改，不合理则抛出自己写的异常
        if(age >= 0 && age <=150) {
            this.age = age;
        }else{
            throw new AgeExceotion("年龄不合理哦！！！");
        }
    }
//    重写toString方法

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", studentId='" + studentId + '\'' +
                ", age=" + age +
                '}';
    }
//    重写equals方法，根据学号进行比较

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Student student = (Student) o;
        return Objects.equals(studentId, student.studentId);
    }

    @Override
    public int hashCode() {
        return Objects.hash(studentId);
    }
}
